package com.wokeMy.view.channe.zhongfu;

import android.app.Dialog;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke.MybankblindActivity;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Data_bindcarlist;
import com.woke.data.UnionPayChannel;
import com.woke.method.MyApp;
import com.woke.serizedatas.Datas_load;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

public class ZhongfuActivity extends BaseActivity implements View.OnClickListener {

    TextView money_zhongfu_tv,zfcard_zhongfu,jscard_zhongfu;
    RelativeLayout zhifu_zhongfu_rl;
    RelativeLayout jieshuan_zhongfu_rl;
    ImageView back;
    Button paybt;

    Data_bindcarlist listdata;
    Intent intent;
    float money;
    int way_id,vip_level;
    String user_id;
    String pay_card_no,card_no;
    UnionPayChannel unionPayChannel;
    Dialog dialog;

    String useName,merchantName;
    int status; //0未入驻 1入驻

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_zhongfu2);
    }

    @Override
    public void initActivity() {
        zhifu_zhongfu_rl.setOnClickListener(this);
        jieshuan_zhongfu_rl.setOnClickListener(this);
        back.setOnClickListener(this);
        paybt.setOnClickListener(this);
        money_zhongfu_tv.setText("￥"+money);
    }

    @Override
    public void initView() {
        money_zhongfu_tv = myFindViewById(R.id.money_zhongfu_tv);
        zhifu_zhongfu_rl = myFindViewById(R.id.zhifu_zhongfu_rl);
        jieshuan_zhongfu_rl = myFindViewById(R.id.jieshuan_zhongfu_rl);
        zfcard_zhongfu = myFindViewById(R.id.zfcard_zhongfu);
        jscard_zhongfu = myFindViewById(R.id.jscard_zhongfu);
        back = myFindViewById(R.id.back_zhongfu_iv);
        paybt = myFindViewById(R.id.pay_zhongfu_bt);
    }

    @Override
    public void initData() {
        intent=getIntent();
        money = intent.getFloatExtra ("money", 0.01f);
        unionPayChannel = (UnionPayChannel)(intent.getSerializableExtra("UnionPayChannel"));
        way_id = unionPayChannel.getWay();
        status = intent.getIntExtra ("status", 0);
        String name="名字为空";
        MyApp myApp=(MyApp)getApplication();
        Datas_load datas_load=myApp.getDatas_load();
        if (!datas_load.getUser_nicename().equals("")){
            name=datas_load.getUser_nicename();
        }else {
            name=datas_load.getCom_name();
        }
        useName=name;
        merchantName=name+"的商户";
        user_id=((MyApp)getApplication()).getDatas_load().getId();
        vip_level=(Integer.parseInt(((MyApp)getApplication()).getDatas_load().getUser_type()));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.money_zhongfu_tv:
                break;
            case R.id.back_zhongfu_iv:
                finish();
                break;
            case R.id.pay_zhongfu_bt:
                if (pay_card_no==null||pay_card_no.equals("")){
                    Toast.makeText(ZhongfuActivity.this,"支付卡未选择",Toast.LENGTH_SHORT).show();
                }else if(card_no==null||card_no.equals("")){
                    Toast.makeText(ZhongfuActivity.this,"结算卡未选择",Toast.LENGTH_SHORT).show();
                }else {
                    dialog=  Util.createLoadingDialog(ZhongfuActivity.this);
                    zfZf();
                }
                //                Toast.makeText(ZhongfuActivity.this,"暂未开通",Toast.LENGTH_SHORT).show();
                break;
            case R.id.zhifu_zhongfu_rl:
                Intent intent = new Intent (ZhongfuActivity.this, MybankblindActivity.class);
                intent.putExtra ("czhiortxian", "zhifucard");
                startActivityForResult (intent, Constant.REQUEST_ZHIFU);
                break;
            case R.id.jieshuan_zhongfu_rl:
                Intent intent2 = new Intent (ZhongfuActivity.this, MybankblindActivity.class);
                intent2.putExtra ("czhiortxian", "zscard");
                startActivityForResult (intent2, Constant.REQUEST_JIESHUAN);
                break;
            default:
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //        Log.e("RESPONSE_JIESHUAN",resultCode+"");
        if (resultCode==Constant.RESPONSE_ZHIFU){
            listdata=(Data_bindcarlist)data.getSerializableExtra("listdata");
            pay_card_no=listdata.cardno;
            zfcard_zhongfu.setText(pay_card_no.substring(0, 4)+"    "+"***"+"    "+"***"+"    "+"***"+"    "+pay_card_no.substring(pay_card_no.length()-3,pay_card_no.length()));
        }else if(resultCode==Constant.RESPONSE_JIESHUAN){
            listdata=(Data_bindcarlist)data.getSerializableExtra("listdata");
            card_no=listdata.cardno;
            jscard_zhongfu.setText(card_no.substring(0, 4)+"    "+"***"+"    "+"***"+"    "+"***"+"    "+card_no.substring(card_no.length()-3,card_no.length()));
        }
    }


    //    中付入驻
    public void rzZhongfu(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);   //通道
        params.put("card_no",pay_card_no);  //储蓄卡
        params.put("subMerName",merchantName);
        params.put("subMerAddr","福建省厦门市思明区虎园路2号");
        Log.e(" 中付入驻（本通道）params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.ZFRZ_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("中付入驻", result);
                    try {
                        Util.closeDialog(dialog);
                        JSONObject  jsonObject =new JSONObject(result);
                        if (jsonObject.getString("flag").equals("success")){
                            Toast.makeText(ZhongfuActivity.this,jsonObject.getString("info"),Toast.LENGTH_SHORT).show();
                        }else {
                            tishi(jsonObject.getString("info"));
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });
    }

    //    中付入驻账号修改
    public void changeRzZhongfu(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);   //通道
        params.put("card_no",pay_card_no);  //储蓄卡
        Log.e(" 中付入驻账号修改params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.CHANGEZFRZ_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("中付入驻账号修改", result);
                    try {
                        Util.closeDialog(dialog);
                        JSONObject  jsonObject =new JSONObject(result);
                        if (jsonObject.getString("flag").equals("success")){
                            Toast.makeText(ZhongfuActivity.this,jsonObject.getString("info"),Toast.LENGTH_SHORT).show();
                        }else {
                            tishi(jsonObject.getString("info"));
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });
    }
    //    中付费率修改
    public void zfFlChange(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);   //通道
        Log.e(" 中付费率修改params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.ZFFLCHANGE_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("中付费率修改", result);
                    try {
                        Util.closeDialog(dialog);
                        JSONObject  jsonObject =new JSONObject(result);
                        if (jsonObject.getString("flag").equals("success")){
                            finish();
                            Toast.makeText(ZhongfuActivity.this,jsonObject.getString("info"),Toast.LENGTH_SHORT).show();
                        }else {
                            tishi(jsonObject.getString("info"));
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });
    }
    //    中付支付
    public void zfZf(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);   //通道
        params.put("amount",money);
        params.put("credit_card",pay_card_no); //支付卡（信用卡）
        params.put("debit_card",card_no); //收款卡（储蓄卡）
        Log.e(" 中付支付url",Constant.ZFPAY_URL);
        Log.e(" 中付支付params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.ZFPAY_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("中付支付", result);
                    try {
                        Util.closeDialog(dialog);
                        JSONObject  jsonObject =new JSONObject(result);
                        String info = jsonObject.getString("info");
                        if (jsonObject.get("flag").equals("success")) {
                            if (jsonObject.has("data")){
                                String  html = jsonObject.getJSONObject("data").getString("data");
                                Intent intent = new Intent(ZhongfuActivity.this, ZfKtzfActivity.class);
                                intent.putExtra("html", html);
                                startActivity(intent);
                            }
                        } else {
                            if (jsonObject.has("data")){
                                info=jsonObject.getJSONObject("data").getJSONObject("data").getString("dealmsg");
                            }
                            tishi(info);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });
    }
}
